package com.ssyc.student.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.ssyc.common.base.LazyBaseFragment;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.manager.SrfManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.student.R;
import com.ssyc.student.adapter.StDepotClothingAdapter;
import com.ssyc.student.bean.DepotInfo;

import java.util.List;


/**
 * 学生端--补给站--服装Fragment
 */

public class StudentDepotClothingFragment extends LazyBaseFragment {

    //下拉刷新控件
    private SmartRefreshLayout srf;
    //列表控件
    private RecyclerView rv;
    //列表的数据集合
    private List<DepotInfo.DataBean.Type2Bean> oldDatas;

    //空视图
    private View emptyView;

    //适配器
    private StDepotClothingAdapter adapter;

    //单例模式
    private static volatile StudentDepotClothingFragment studentDepotClothingFragment;

    public static StudentDepotClothingFragment newInstance(String data) {
//        if (studentDepotClothingFragment == null) {
//            synchronized (StudentDepotClothingFragment.class) {
//                if (studentDepotClothingFragment == null) {
//                    studentDepotClothingFragment = new StudentDepotClothingFragment();
//                    //通过Bundle传递页面需要展示的集合数据的json
//                    Bundle bundle = new Bundle();
//                    bundle.putString("data", data);
//                    studentDepotClothingFragment.setArguments(bundle);
//                }
//            }
//        }
        studentDepotClothingFragment = new StudentDepotClothingFragment();
        //通过Bundle传递页面需要展示的集合数据的json
        Bundle bundle = new Bundle();
        bundle.putString("data", data);
        studentDepotClothingFragment.setArguments(bundle);
        return studentDepotClothingFragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_student_depot_clothing;
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {

        showContent();

        //初始化视图
        initView(view);

        //设置下拉控件
        initSrl();

        //获取通过Activity传递过过来的页面数据
        initData();

        //设置rv
        initRv();
    }

    /**
     * 设置下拉刷新控件
     */
    private void initSrl() {
        SrfManager.setSmartByPullListener(srf, null);
    }

    /**
     * 初始化View
     */
    private void initView(View view) {
        srf = view.findViewById(R.id.srf);
        rv = view.findViewById(R.id.rv);
    }

    /**
     * 获取页面数据的方法
     */
    private void initData() {
        String data = getArguments().getString("data");
        if (!TextUtils.isEmpty(data)) {
            try {
                oldDatas = GsonUtil.jsonToList(data, DepotInfo.DataBean.Type2Bean[].class);
            }catch (Exception e){
                Log.i("test",e.toString());
            }
        }
    }

    /**
     * 设置列表
     */
    private void initRv() {
        emptyView = View.inflate(getContext(), R.layout.base_rv_common_empty_view, null);
        adapter = new StDepotClothingAdapter(getActivity(), getContext(), R.layout.student_rv_depot_clothing, oldDatas);
        //设置横向的布局管理器
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);
        BqaManager.setRv(emptyView, gridLayoutManager, getContext(), adapter, rv, null);
    }

    /**
     * EventBus刷新数据
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        super.busEvent(busInfo);
        if (null != busInfo && busInfo.useMoney != 0) {
            adapter.notifyDataSetChanged();
        }
    }
}
